home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_libgtop.idb / usr / freeware / doc / libgtop / second.c.z / second.c
C/C++ Source or Header  |  2002-07-08  |  12KB  |  377 lines

  1. /* $Id: second.c,v 1.20.4.3 1999/09/16 21:08:55 martin Exp $ */
  2.  
  3. /* Copyright (C) 1998-99 Martin Baulig
  4.    This file is part of LibGTop 1.0.
  5.  
  6.    Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
  7.  
  8.    LibGTop is free software; you can redistribute it and/or modify it
  9.    under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2 of the License,
  11.    or (at your option) any later version.
  12.  
  13.    LibGTop is distributed in the hope that it will be useful, but WITHOUT
  14.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15.    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16.    for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with LibGTop; see the file COPYING. If not, write to the
  20.    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21.    Boston, MA 02111-1307, USA.
  22. */
  23.  
  24. #include <locale.h>
  25.  
  26. #include <glibtop.h>
  27. #include <glibtop/open.h>
  28. #include <glibtop/close.h>
  29. #include <glibtop/xmalloc.h>
  30.  
  31. #include <glibtop/union.h>
  32. #include <glibtop/sysdeps.h>
  33.  
  34. static void
  35. output (pid_t pid)
  36. {
  37.     glibtop_union data;
  38.     char *args;
  39.     unsigned i;
  40.  
  41. #if HAVE_LIBGTOP_SMP
  42.     unsigned long total;
  43.     double p_total, p_utime, p_stime;
  44.     double b_total, b_utime, b_stime;
  45.     double s_total, s_utime, s_stime;
  46.     double my_utime, my_stime;
  47.     int ncpu;
  48. #endif
  49.  
  50.     printf ("\n");
  51.         
  52.     glibtop_get_proc_state (&data.proc_state, pid);
  53.  
  54. #if LIBGTOP_VERSION_CODE > 1001000        
  55.     printf ("Proc_State   PID  %5d (0x%08lx): '%s', %u - "
  56.         "%u, %u, %u, %u - %u, %u, %u\n", (int) pid,
  57.         (unsigned long) data.proc_state.flags,
  58.         data.proc_state.cmd, data.proc_state.state,
  59.         data.proc_state.uid, data.proc_state.gid,
  60.         data.proc_state.ruid, data.proc_state.rgid,
  61.         data.proc_state.has_cpu, data.proc_state.processor,
  62.         data.proc_state.last_processor);
  63. #else
  64.     printf ("Proc_State   PID  %5d (0x%08lx): "
  65.         "'%s', %c, %lu, %lu\n", (int) pid,
  66.         (unsigned long) data.proc_state.flags,
  67.         data.proc_state.cmd, data.proc_state.state,
  68.         (unsigned long) data.proc_state.uid,
  69.         (unsigned long) data.proc_state.gid);
  70. #endif
  71.         
  72.     glibtop_get_proc_uid (&data.proc_uid, pid);
  73.     
  74. #if LIBGTOP_VERSION_CODE > 1001000    
  75.     printf ("Proc_Uid     PID  %5d (0x%08lx): "
  76.         "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d - %d",
  77.         (int) pid, (unsigned long) data.proc_uid.flags,
  78.         data.proc_uid.uid, data.proc_uid.euid,
  79.         data.proc_uid.gid, data.proc_uid.egid,
  80.         data.proc_uid.suid, data.proc_uid.sgid,
  81.         data.proc_uid.fsuid, data.proc_uid.fsgid,
  82.         data.proc_uid.pid, data.proc_uid.ppid,
  83.         data.proc_uid.pgrp, data.proc_uid.session,
  84.         data.proc_uid.tty, data.proc_uid.tpgid,
  85.         data.proc_uid.priority, data.proc_uid.nice,
  86.         data.proc_uid.ngroups);
  87.         
  88.     if (data.proc_uid.ngroups) {
  89.         for (i = 0; i < data.proc_uid.ngroups; i++) {
  90.             if (i)
  91.                 printf (" %d", data.proc_uid.groups [i]);
  92.             else
  93.                 printf (" (%d", data.proc_uid.groups [i]);
  94.         }
  95.         printf (")");
  96.     }
  97.  
  98.     printf ("\n");
  99. #else
  100.     printf ("Proc_Uid     PID  %5d (0x%08lx): "
  101.         "%d %d %d %d %d %d %d %d %d %d %d %d\n", (int) pid,
  102.         (unsigned long) data.proc_uid.flags,
  103.         data.proc_uid.uid, data.proc_uid.euid,
  104.         data.proc_uid.gid, data.proc_uid.egid,
  105.         data.proc_uid.pid, data.proc_uid.ppid,
  106.         data.proc_uid.pgrp, data.proc_uid.session,
  107.         data.proc_uid.tty, data.proc_uid.tpgid,
  108.         data.proc_uid.priority, data.proc_uid.nice);
  109. #endif
  110.  
  111.     glibtop_get_proc_mem (&data.proc_mem, pid);
  112.         
  113.     printf ("Proc_Mem     PID  %5d (0x%08lx): "
  114.         "%lu %lu %lu %lu %lu %lu\n", (int) pid,
  115.         (unsigned long) data.proc_mem.flags,
  116.         (unsigned long) data.proc_mem.size,
  117.         (unsigned long) data.proc_mem.vsize,
  118.         (unsigned long) data.proc_mem.resident,
  119.         (unsigned long) data.proc_mem.share,
  120.         (unsigned long) data.proc_mem.rss,
  121.         (unsigned long) data.proc_mem.rss_rlim);
  122.         
  123.     glibtop_get_proc_segment (&data.proc_segment, pid);
  124.  
  125. #if LIBGTOP_VERSION_CODE > 1001000
  126.     printf ("Proc_Segment PID  %5d (0x%08lx): "
  127.         "%lu %lu %lu %lu %lu 0x%lx 0x%lx 0x%lx "
  128.         "0x%lx 0x%lx 0x%lx 0x%lx 0x%lx "
  129.         "0x%lx 0x%lx 0x%lx 0x%lx\n", (int) pid,
  130.         (unsigned long) data.proc_segment.flags,
  131.         (unsigned long) data.proc_segment.text_rss,
  132.         (unsigned long) data.proc_segment.shlib_rss,
  133.         (unsigned long) data.proc_segment.data_rss,
  134.         (unsigned long) data.proc_segment.stack_rss,
  135.         (unsigned long) data.proc_segment.dirty_size,
  136.         (unsigned long) data.proc_segment.start_code,
  137.         (unsigned long) data.proc_segment.end_code,
  138.         (unsigned long) data.proc_segment.start_data,
  139.         (unsigned long) data.proc_segment.end_data,
  140.         (unsigned long) data.proc_segment.start_brk,
  141.         (unsigned long) data.proc_segment.end_brk,
  142.         (unsigned long) data.proc_segment.start_stack,
  143.         (unsigned long) data.proc_segment.start_mmap,
  144.         (unsigned long) data.proc_segment.arg_start,
  145.         (unsigned long) data.proc_segment.arg_end,
  146.         (unsigned long) data.proc_segment.env_start,
  147.         (unsigned long) data.proc_segment.env_end);
  148. #else
  149.     printf ("Proc_Segment PID  %5d (0x%08lx): "
  150.         "%lu %lu %lu %lu %lu %lu %lu %lu\n", (int) pid,
  151.         (unsigned long) data.proc_segment.flags,
  152.         (unsigned long) data.proc_segment.text_rss,
  153.         (unsigned long) data.proc_segment.shlib_rss,
  154.         (unsigned long) data.proc_segment.data_rss,
  155.         (unsigned long) data.proc_segment.stack_rss,
  156.         (unsigned long) data.proc_segment.dirty_size,
  157.         (unsigned long) data.proc_segment.start_code,
  158.         (unsigned long) data.proc_segment.end_code,
  159.         (unsigned long) data.proc_segment.start_stack);
  160. #endif
  161.  
  162.     glibtop_get_proc_time (&data.proc_time, pid);
  163.         
  164.     printf ("Proc_Time    PID  %5d (0x%08lx): "
  165.         "%lu %lu %lu %lu %lu %lu %lu %lu %lu\n", (int) pid,
  166.         (unsigned long) data.proc_time.flags,
  167.         (unsigned long) data.proc_time.start_time,
  168.         (unsigned long) data.proc_time.rtime,
  169.         (unsigned long) data.proc_time.utime,
  170.         (unsigned long) data.proc_time.stime,
  171.         (unsigned long) data.proc_time.cutime,
  172.         (unsigned long) data.proc_time.cstime,
  173.         (unsigned long) data.proc_time.timeout,
  174.         (unsigned long) data.proc_time.it_real_value,
  175.         (unsigned long) data.proc_time.frequency);
  176.  
  177.     glibtop_get_proc_signal (&data.proc_signal, pid);
  178.     
  179. #if LIBGTOP_VERSION_CODE > 1001000
  180.     printf ("Proc_Signal  PID  %5d (0x%08lx): "
  181.         "%lx %lx %lx %lx %lx %lx %lx %lx\n", (int) pid,
  182.         (unsigned long) data.proc_signal.flags,
  183.         (unsigned long) data.proc_signal.signal [0],
  184.         (unsigned long) data.proc_signal.signal [1],
  185.         (unsigned long) data.proc_signal.blocked [0],
  186.         (unsigned long) data.proc_signal.blocked [1],
  187.         (unsigned long) data.proc_signal.sigignore [0],
  188.         (unsigned long) data.proc_signal.sigignore [1],
  189.         (unsigned long) data.proc_signal.sigcatch [0],
  190.         (unsigned long) data.proc_signal.sigcatch [1]);
  191. #else
  192.     printf ("Proc_Signal  PID  %5d (0x%08lx): "
  193.         "%lu %lu %lu %lu\n", (int) pid,
  194.         (unsigned long) data.proc_signal.flags,
  195.         (unsigned long) data.proc_signal.signal,
  196.         (unsigned long) data.proc_signal.blocked,
  197.         (unsigned long) data.proc_signal.sigignore,
  198.         (unsigned long) data.proc_signal.sigcatch);
  199. #endif
  200.  
  201.     glibtop_get_proc_kernel (&data.proc_kernel, pid);
  202.  
  203.     printf ("Proc_Kernel  PID  %5d (0x%08lx): "
  204.         "%lu %lu %lu %lu %lu 0x%lx 0x%lx 0x%lx (%s)\n", (int) pid,
  205.         (unsigned long) data.proc_kernel.flags,
  206.         (unsigned long) data.proc_kernel.k_flags,
  207.         (unsigned long) data.proc_kernel.min_flt,
  208.         (unsigned long) data.proc_kernel.maj_flt,
  209.         (unsigned long) data.proc_kernel.cmin_flt,
  210.         (unsigned long) data.proc_kernel.cmaj_flt,
  211.         (unsigned long) data.proc_kernel.kstk_esp,
  212.         (unsigned long) data.proc_kernel.kstk_eip,
  213.         (unsigned long) data.proc_kernel.nwchan,
  214.         data.proc_kernel.wchan);
  215.  
  216.     printf ("\n");
  217.  
  218.     args = glibtop_get_proc_args (&data.proc_args, pid, 0);
  219.  
  220.     for (i = 0; i < data.proc_args.size; i++) {
  221.         if (args [i]) continue;
  222.         args [i] = '|';
  223.     }
  224.  
  225.     printf ("Proc_Args    PID  %5d (0x%08lx): %lu - '%s'\n", (int) pid,
  226.         (unsigned long) data.proc_args.flags,
  227.         (unsigned long) data.proc_args.size,
  228.         args ? args : "");
  229.  
  230.     glibtop_free (args);
  231.  
  232.     printf ("\n");
  233.  
  234. #if HAVE_LIBGTOP_SMP
  235.     ncpu = glibtop_global_server->ncpu;
  236.  
  237.     glibtop_get_proc_time (&data.proc_time, pid);
  238.         
  239.     total = (unsigned long) data.proc_time.utime +
  240.         (unsigned long) data.proc_time.stime;
  241.  
  242.     p_total = total ? (double) total : 1.0;
  243.  
  244.     p_utime = (double) data.proc_time.utime * 100.0 / p_total;
  245.     p_stime = (double) data.proc_time.stime * 100.0 / p_total;
  246.  
  247.     b_total = p_total / ncpu;
  248.     b_utime = (double) data.proc_time.utime / ncpu;
  249.     b_stime = (double) data.proc_time.stime / ncpu;
  250.  
  251.     s_total = 0.0; s_utime = 0.0; s_stime = 0.0;
  252.  
  253.     printf ("Proc_Time    PID  %5d (0x%08lx): %12lu   %12lu   %12lu\n", (int) pid,
  254.         (unsigned long) data.proc_time.flags, total,
  255.         (unsigned long) data.proc_time.utime,
  256.         (unsigned long) data.proc_time.stime);
  257.  
  258.     for (i = 0; i < ncpu; i++) {
  259.         unsigned long this_total;
  260.  
  261.         this_total = (unsigned long) data.proc_time.xcpu_utime [i] +
  262.             (unsigned long) data.proc_time.xcpu_stime [i];
  263.  
  264.         printf ("CPU %3d      PID  %5d (0x%08lx): %12lu   %12lu   %12lu\n", i,
  265.             (int) pid, (unsigned long) data.proc_time.flags, this_total,
  266.             (unsigned long) data.proc_time.xcpu_utime [i],
  267.             (unsigned long) data.proc_time.xcpu_stime [i]);
  268.  
  269.         s_total += fabs (((double) this_total) - b_total);
  270.         s_utime += fabs (((double) data.proc_time.xcpu_utime [i]) - b_utime);
  271.         s_stime += fabs (((double) data.proc_time.xcpu_stime [i]) - b_stime);
  272.     }
  273.  
  274.     printf ("\n");
  275.  
  276.     printf ("Proc_Time    PID  %5d (0x%08lx): %12.3f   %12.3f   %12.3f\n", (int) pid,
  277.         (unsigned long) data.proc_time.flags, 100.0, p_utime, p_stime);
  278.  
  279.     for (i = 0; i < ncpu; i++) {
  280.         double this_p_total, this_p_utime, this_p_stime;
  281.         unsigned long this_total;
  282.  
  283.         this_total = (unsigned long) data.proc_time.xcpu_utime [i] +
  284.             (unsigned long) data.proc_time.xcpu_stime [i];
  285.  
  286.         this_p_total = (double) this_total * 100.0 / p_total;
  287.  
  288.         this_p_utime = (double) data.proc_time.xcpu_utime [i] * 100.0 / p_total;
  289.         this_p_stime = (double) data.proc_time.xcpu_stime [i] * 100.0 / p_total;
  290.  
  291.         printf ("CPU %3d      PID  %5d (0x%08lx): %12.3f   %12.3f   %12.3f\n", i,
  292.             (int) pid, (unsigned long) data.proc_time.flags,
  293.             this_p_total, this_p_utime, this_p_stime);
  294.     }
  295.  
  296.     printf ("\n");
  297.  
  298.     my_utime = (unsigned long) data.proc_time.utime ?
  299.         (double) data.proc_time.utime : 1.0;
  300.     my_stime = (unsigned long) data.proc_time.stime ?
  301.         (double) data.proc_time.stime : 1.0;
  302.  
  303.     printf ("SPIN: %31s %12.3f   %12.3f   %12.3f\n", "", s_total * 100.0 / p_total,
  304.         s_utime * 100.0 / my_utime, s_stime * 100.0 / my_stime);
  305.  
  306.     printf ("\n");
  307. #endif
  308. }
  309.  
  310. int
  311. main (int argc, char *argv [])
  312. {
  313.     glibtop_proclist proclist;
  314.     glibtop_sysdeps sysdeps;
  315.     unsigned *ptr, pid, i;
  316.  
  317.     setlocale (LC_ALL, "");
  318.     bindtextdomain (PACKAGE, GTOPLOCALEDIR);
  319.     textdomain (PACKAGE);
  320.     
  321.     glibtop_init ();
  322.  
  323.     glibtop_get_sysdeps (&sysdeps);
  324.  
  325.     printf ("Sysdeps      (0x%08lx): %lu, %lu, %lu, %lu, %lu, "
  326.         "%lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu\n",
  327.         (unsigned long) sysdeps.flags,
  328.         (unsigned long) sysdeps.cpu,
  329.         (unsigned long) sysdeps.mem,
  330.         (unsigned long) sysdeps.swap,
  331.         (unsigned long) sysdeps.uptime,
  332.         (unsigned long) sysdeps.loadavg,
  333.         (unsigned long) sysdeps.shm_limits,
  334.         (unsigned long) sysdeps.msg_limits,
  335.         (unsigned long) sysdeps.sem_limits,
  336.         (unsigned long) sysdeps.proclist,
  337.         (unsigned long) sysdeps.proc_state,
  338.         (unsigned long) sysdeps.proc_uid,
  339.         (unsigned long) sysdeps.proc_mem,
  340.         (unsigned long) sysdeps.proc_time,
  341.         (unsigned long) sysdeps.proc_signal,
  342.         (unsigned long) sysdeps.proc_kernel,
  343.         (unsigned long) sysdeps.proc_segment);
  344.  
  345.     if ((argc == 2) && (sscanf (argv [1], "%d", &pid) == 1)) {
  346.         output (pid);
  347.  
  348.         exit (0);
  349.     }
  350.  
  351.     printf ("\n");
  352.  
  353.     ptr = glibtop_get_proclist (&proclist, 0, 0);
  354.  
  355.     printf ("Proclist     (0x%08lx): %lu, %lu, %lu\n",
  356.         (unsigned long) proclist.flags,
  357.         (unsigned long) proclist.number,
  358.         (unsigned long) proclist.size,
  359.         (unsigned long) proclist.total);
  360.  
  361.     if (!ptr) exit (1);
  362.  
  363.     for (i = 0; i < proclist.number; i++) {
  364.  
  365.         pid = ptr [i];
  366.         
  367.         output (pid);
  368.     }
  369.  
  370.     glibtop_free (ptr);
  371.  
  372.     exit (0);
  373. }
  374.  
  375.  
  376.  
  377.